package com.team.ai.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.team.ai.controller.form.AdminiLoginForm;
import com.team.ai.controller.form.LoginForm;
import com.team.ai.controller.vo.AdminiLoginVO;
import com.team.ai.controller.vo.UserLoginVO;
import com.team.ai.entity.Administrator;
import com.team.ai.entity.ResultEntity;
import com.team.ai.service.AdminiService;
import com.team.ai.service.util.Result;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("v2/Admini/")
public class AdminiController {
    private static final Logger logger = LoggerFactory.getLogger(BookController.class);//日志getLogger需要传入的类名
    @Resource
    private RedisTemplate<String, String> redisTemplate;//Redis工具类

    @Resource
    AdminiService adminiService;

    @PostMapping("login")
    private Result<AdminiLoginVO> login(@RequestBody AdminiLoginForm form){
        Administrator administrator= adminiService.login(form.getName(), form.getPwd());
        AdminiLoginVO adminiLoginVO =new AdminiLoginVO();
        BeanUtils.copyProperties(administrator, adminiLoginVO);
        return Result.ok(adminiLoginVO);
    }
    @PostMapping("insert")
    private String insert(
            @RequestParam(value = "name",defaultValue = "-1") String name,
            @RequestParam(value = "pwd",defaultValue = "-1") String pwd,
            @RequestParam(value = "phone",defaultValue = "-1") String phone,
            @RequestParam(value = "email",defaultValue = "-1") String email,
            @RequestParam(value = "age",defaultValue = "-1") int age

    ){
        return adminiService.insert(name, pwd, phone, email, age);
    }
    @DeleteMapping("delete")
    private String delete(
            @RequestParam(value = "id",defaultValue = "-1") int id

    ){
        if (id==-1){
            return ResultEntity.fail(-10002);
        }
        return adminiService.delete(id);
    }

    @PostMapping("update")
    private String update(
            @RequestParam(value = "name",defaultValue = "-1") String name,
            @RequestParam(value = "pwd",defaultValue = "-1") String pwd,
            @RequestParam(value = "phone",defaultValue = "-1") String phone,
            @RequestParam(value = "email",defaultValue = "-1") String email,
            @RequestParam(value = "age",defaultValue = "-1") int age,
            @RequestParam(value = "id",defaultValue = "-1") int id

    ){
        return adminiService.update(name, pwd, phone, email, age,id);
    }
    @GetMapping("selectList")
    private String selectList(

    ){
        return  adminiService.selectList();
    }
    @GetMapping("selectByPrimaryKey")
    private String selectByPrimaryKey( @RequestParam(value = "id",defaultValue = "-1") int id

    ){
        return  adminiService.selectByPrimaryKey(id);
    }
}
